package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.*;
import com.igeekhome.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工作订单 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/workOrder")
public class WorkOrderController {

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private IWorkOrderService iWorkOrderService;

    @Autowired
    private ICustomerServiceService iCustomerServiceService;

    @Autowired
    private IWorkLogService iWorkLogService;

    @Autowired
    private IWorkOrderReplyService iWorkOrderReplyService;
    /**
     * 全部工单
     * @param m
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/gongdan")
    public String workorder(Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
//        ArrayList<WorkOrder> arrayList=new ArrayList<>();
//        arrayList= (ArrayList<WorkOrder>) iWorkOrderService.list();

        IPage<WorkOrder> page=new Page<>(current,size);
        QueryWrapper<WorkOrder> qu=new QueryWrapper<>();
        qu.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        qu.orderByDesc("updatetime");
        IPage<WorkOrder> page1=iWorkOrderService.page(page,qu);
//        IPage<CustomerInfo> page1 = iCustomerInfoService.page(page, qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();
        ArrayList<CustomerInfo>list= (ArrayList<CustomerInfo>) iCustomerInfoService.list();
        m.addAttribute("lists",list);

        /**
         * 查询客服名和客户名
         */
        for(WorkOrder wo:arrayList){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("customerserviceid",wo.getCustomerserviceid());
            CustomerService customerService=iCustomerServiceService.getOne(qw);
            if(customerService!=null){
                wo.setCustomerservicename(customerService.getRealname());
            }

        }

        for(WorkOrder wo:arrayList){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("id",wo.getCustomerid());
//            wo.setCustomerservicename(iCustomerServiceService.getOne(qw).getRealname());
            CustomerInfo customerInfo=iCustomerInfoService.getOne(qw);
            if(customerInfo!=null){
                wo.setCustomername(customerInfo.getRealname());
            }
        }

            m.addAttribute("pagesCount",pagesCount);
            m.addAttribute("list",arrayList);
            m.addAttribute("gd","全部工单");
            return "forward:/workorder";
    }


    /**
     * 待处理
     * @param m
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/daichuli")
    public String daichuli(Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        QueryWrapper<WorkOrder> qw=new QueryWrapper();
        qw.like("state","待处理");
        qw.like("title", value);
//        qw.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        IPage<WorkOrder> page=new Page<>(current,size);
        IPage<WorkOrder>page1=iWorkOrderService.page(page,qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();

        /**
         * 查询客服名和客户名
         */
        for(WorkOrder wo:arrayList){
            QueryWrapper q=new QueryWrapper();
            q.eq("customerserviceid",wo.getCustomerserviceid());
            CustomerService customerService=iCustomerServiceService.getOne(q);
            if(customerService!=null){
                wo.setCustomerservicename(customerService.getRealname());
            }

        }

        for(WorkOrder wo:arrayList){
            QueryWrapper q=new QueryWrapper();
            q.eq("id",wo.getCustomerid());
            CustomerInfo customerInfo=iCustomerInfoService.getOne(q);
            if(customerInfo!=null){
                wo.setCustomername(customerInfo.getRealname());
            }
        }
        m.addAttribute("pagesCount",pagesCount);
        m.addAttribute("gd","待处理的");
        m.addAttribute("list",arrayList);
        return "forward:/daichuli";
    }

    /**
     *
     * 我创建的
     * @param session
     * @param m
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/chuangjian")
    public String IChuangJian(HttpSession session,Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        QueryWrapper<WorkOrder> qw=new QueryWrapper();
        CustomerService customerService = new CustomerService();
        customerService= (CustomerService) session.getAttribute("cs");
        qw.eq("customerserviceid",customerService.getCustomerserviceid());
        qw.like("title", value);
//        qw.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        IPage<WorkOrder> page=new Page<>(current,size);
        IPage<WorkOrder>page1=iWorkOrderService.page(page,qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();

        for(WorkOrder wo:arrayList){
            QueryWrapper q=new QueryWrapper();
            q.eq("customerserviceid",wo.getCustomerserviceid());
            CustomerService Cs=iCustomerServiceService.getOne(q);
            if(Cs!=null){
                wo.setCustomerservicename(Cs.getRealname());
            }

        }

        for(WorkOrder wo:arrayList){
            QueryWrapper q=new QueryWrapper();
            q.eq("id",wo.getCustomerid());
//            wo.setCustomerservicename(iCustomerServiceService.getOne(qw).getRealname());
            CustomerInfo customerInfo=iCustomerInfoService.getOne(q);
            if(customerInfo!=null){
                wo.setCustomername(customerInfo.getRealname());
            }
        }
        m.addAttribute("pagesCount",pagesCount);
        m.addAttribute("gd","我创建的");
        m.addAttribute("list",arrayList);
        return "forward:/chuangjian";
    }

    /**
     * 详情操作
     * @param m
     * @param workOrder
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Model m,WorkOrder workOrder){
        QueryWrapper<WorkOrder>qw=new QueryWrapper<>();
        qw.eq("id",workOrder.getId());
        WorkOrder wo=iWorkOrderService.getOne(qw);
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.eq("id",workOrder.getCustomerid());
        CustomerInfo customerInfo=iCustomerInfoService.getById(wo.getCustomerid());
        if(customerInfo!=null){
            wo.setCustomername(customerInfo.getRealname());
        }
        CustomerService customerService=iCustomerServiceService.getById(wo.getCustomerserviceid());
        if(customerService!=null){
            wo.setCustomerservicename(customerService.getRealname());
        }
        m.addAttribute("gd","工单详情");

        m.addAttribute("list",wo);
        return "detail";
    }

    /**
     * 抄送我的
     * @param session
     * @param m
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/caosong")
    public String caosong(HttpSession session, Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        IPage<WorkOrder> page=new Page<>(current,size);
        CustomerService customerService=new CustomerService();
        QueryWrapper<WorkOrder> qw=new QueryWrapper<>();
        customerService= (CustomerService) session.getAttribute("cs");
        qw.eq("customerserviceid",customerService.getCustomerserviceid());
        qw.like("title", value);
//        qw.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        IPage<WorkOrder>page1=iWorkOrderService.page(page,qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();

//        ArrayList<WorkOrder>arrayList= (ArrayList<WorkOrder>) iWorkOrderService.list(qw);
        m.addAttribute("list",arrayList);
        m.addAttribute("gd","抄送我的");
        m.addAttribute("pagesCount",pagesCount);
        return "caosong";
    }

    /**
     * 添加工单
     * @param workOrder
     * @param session
     * @param m
     * @return
     */
    @RequestMapping("/tianjia")
    public String tianjia(WorkOrder workOrder,HttpSession session,Model m){

        CustomerService customerService =new CustomerService();
        customerService= (CustomerService) session.getAttribute("cs");
        workOrder.setCustomerserviceid(customerService.getCustomerserviceid());
//        LocalDateTime createtime= new LocalDateTime();
        LocalDateTime createtime = LocalDateTime.now();
        workOrder.setCreatetime(createtime);
        workOrder.setUpdatetime(createtime);
        workOrder.setChannel("手动录入");
        this.iWorkOrderService.save(workOrder);
        return "forward:/workOrder/gongdan?value=&current=1&size=5";
    }

    /**
     * 我处理的
     * @param session
     * @param m
     * @return
     */

    @RequestMapping("/chuli")
    public String chuli(HttpSession session, Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        CustomerService customerService =new CustomerService();
        customerService= (CustomerService) session.getAttribute("cs");
        QueryWrapper<WorkOrder>qw=new QueryWrapper<>();
        qw.eq("customerserviceid",customerService.getCustomerserviceid());
        qw.like("title", value);
//        qw.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        qw.notLike("state","未分配");

        IPage<WorkOrder> page=new Page<>(current,size);
        IPage<WorkOrder>page1=iWorkOrderService.page(page,qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();

//        ArrayList<WorkOrder>arrayList= (ArrayList<WorkOrder>) iWorkOrderService.list(qw);
        m.addAttribute("list",arrayList);
        m.addAttribute("gd","我处理的");
        m.addAttribute("pagesCount",pagesCount);
        return "caosong";
    }

    /**
     * 我解决的
     * @param session
     * @param m
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/jiejue")
    public String jiejue(HttpSession session, Model m, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        CustomerService customerService =new CustomerService();
        customerService= (CustomerService) session.getAttribute("cs");
        QueryWrapper<WorkOrder>qw=new QueryWrapper<>();
        qw.eq("customerserviceid",customerService.getCustomerserviceid());
        qw.like("title", value);
//        qw.like("title", value).or().like("worktype",value).or().like("state",value).or().like("priority",value);
        qw.like("state","已解决");

        IPage<WorkOrder> page=new Page<>(current,size);
        IPage<WorkOrder>page1=iWorkOrderService.page(page,qw);
        List<WorkOrder> arrayList= page1.getRecords();
        long pagesCount = page1.getPages();

//        ArrayList<WorkOrder>arrayList= (ArrayList<WorkOrder>) iWorkOrderService.list(qw);
        m.addAttribute("list",arrayList);
        m.addAttribute("gd","我解决的");
        m.addAttribute("pagesCount",pagesCount);
        return "caosong";
    }


    @RequestMapping("/pre")
    public String pre(WorkOrder workOrder,Model m){
        QueryWrapper<WorkOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",workOrder.getId());
        WorkOrder wo=iWorkOrderService.getOne(queryWrapper);
        m.addAttribute("wo",wo);
        m.addAttribute("ID",wo.getId());
        return "bianji";
    }
    @RequestMapping("/bianji")
    public String bianji(WorkOrder workOrder,Model m,HttpSession session) throws UnknownHostException {
        LocalDateTime updatetime = LocalDateTime.now();
        workOrder.setUpdatetime(updatetime);
        workOrder.setChannel("手动编辑");
        QueryWrapper qw=new QueryWrapper();
        qw.eq("id",workOrder.getId());
        this.iWorkOrderService.update(workOrder,qw);

        //工作日志操作
        WorkLog workLog=new WorkLog();
        workLog.setOperatoraction("编辑");
        workLog.setOperatorclass("工单#"+workOrder.getId());
        workLog.setOperatortime(updatetime);
        CustomerService customerService= (CustomerService) session.getAttribute("cs");
        workLog.setOperateperson(customerService.getNickname());
        // 获取IP地址
        String ip = InetAddress.getLocalHost().getHostAddress();
        workLog.setOperatorip(ip);
        iWorkLogService.save(workLog);

        return "forward:/workOrder/gongdan?current=1&size=5&value=";
    }

    /**
     * 接单操作
     * @param workOrder
     * @return
     */
    @RequestMapping("/jiedan")
    public String jiedan(WorkOrder workOrder,HttpSession session) throws UnknownHostException {
        WorkOrder wo=new WorkOrder();
        wo = iWorkOrderService.getById(workOrder.getId());
        wo.setState("待处理");
        LocalDateTime newLDT = LocalDateTime.now();
        wo.setUpdatetime(newLDT);
        iWorkOrderService.updateById(wo);

        LocalDateTime updatetime = LocalDateTime.now();
        //工作日志操作
        WorkLog workLog=new WorkLog();
        workLog.setOperatoraction("接单");
        workLog.setOperatorclass("工单#"+workOrder.getId());
        workLog.setOperatortime(updatetime);
        CustomerService customerService= (CustomerService) session.getAttribute("cs");
        workLog.setOperateperson(customerService.getNickname());
        // 获取IP地址
        String ip = InetAddress.getLocalHost().getHostAddress();
        workLog.setOperatorip(ip);
        iWorkLogService.save(workLog);

        return "forward:/workOrder/gongdan?current=1&size=5&value=";
    }

    @RequestMapping("/rizhi")
    public String rizhi(WorkOrder workOrder,Model m){
        QueryWrapper<WorkLog> qw=new QueryWrapper<>();
        String s="工单#"+workOrder.getId();
        System.out.println(s);
        qw.eq("operatorclass",s);
        ArrayList<WorkLog>arrayList=new ArrayList<>();
        arrayList= (ArrayList<WorkLog>) iWorkLogService.list(qw);

        m.addAttribute("list",arrayList);
        m.addAttribute("gd","操作日志 >> 工单#"+workOrder.getId());
       return "rizhi";
    }

    //这里的id是工单id
    @RequestMapping("/huifu")
    public String huifu(@RequestParam("id") Integer id,HttpSession session,Model m){
        QueryWrapper<WorkOrderReply>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("workorderid",id);
        ArrayList<WorkOrderReply>arrayList=new ArrayList<>();
        arrayList= (ArrayList<WorkOrderReply>) iWorkOrderReplyService.list(queryWrapper);
        m.addAttribute("list",arrayList);
        session.setAttribute("replyCS",id);
        return "huifu";
    }

    @RequestMapping("/commit")
    public String commit(HttpSession session, Model m,WorkOrderReply workOrderReply){
        CustomerService customerService=new CustomerService();
        customerService= (CustomerService) session.getAttribute("cs");
        workOrderReply.setCustomerserviceid(customerService.getCustomerserviceid());
        LocalDateTime time=LocalDateTime.now();
        workOrderReply.setReplytime(time);
        workOrderReply.setWorkorderid((Integer) session.getAttribute("replyCS"));
//        int workOrderId= (int) session.getAttribute("replyCS");
        iWorkOrderReplyService.save(workOrderReply);
        System.out.println("成功");
        return "forward:/workOrder/huifu?value=&id="+workOrderReply.getWorkorderid();
    }

    @RequestMapping("/search")
    public String search(@RequestParam("value") String value,Model m,HttpSession session){
        QueryWrapper<WorkOrderReply>queryWrapper=new QueryWrapper<>();
        int workOrderId= (int) session.getAttribute("replyCS");
        queryWrapper.eq("workorderid",workOrderId);
        ArrayList<WorkOrderReply>arrayList=new ArrayList<>();
        queryWrapper.like("customerserviceid",value).or().like("content",value);
        arrayList= (ArrayList<WorkOrderReply>) iWorkOrderReplyService.list(queryWrapper);
        m.addAttribute("list",arrayList);
//        session.setAttribute("replyCS",id);
        m.addAttribute("gd","回复 >> 工单"+workOrderId);
        return "huifu";
    }

}

